#pragma once

#include "file.h"
#include "stream.h"
#include "task.h"
#include <cstddef>
#include <span>
#include <unistd.h>
#include <utility>

namespace co_async {
class FileBuf {
public:
  FileBuf() : m_file(-1) {}
  FileBuf(FileHandle &&file) : m_file(std::move(file)) {}
  FileBuf(FileBuf &&that) : m_file(std::move(that.m_file)){};
  FileBuf &operator=(FileBuf &&that) {
    m_file = std::move(that.m_file);
    return *this;
  };

  Task<std::size_t> read(std::span<char> buf) { return m_file.read(buf); }
  Task<std::size_t> write(std::span<char> buf) { return m_file.write(buf); }
  void release() noexcept { m_file.release(); }

private:
  FileHandle m_file;
};

using FileStream = IOStream<FileBuf>;
} // namespace co_async
